home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / e / mailinglists / amigae.0993sept.archive / 000028_crash!kirk.safb.af.mil!BWILLS_Sun, 12 Sep 93 02:22:44 PST.msg < prev    next >
Internet Message Format  |  1994-05-26  |  5KB

  1. Received: by bkhouse.cts.com (V1.16/Amiga)
  2.     id AA00000; Sun, 12 Sep 93 02:22:44 PST
  3. Received: from kirk.safb.af.mil by crash.cts.com with smtp
  4.     (Smail3.1.28.1 #18) id m0obbi8-0000YPC; Sat, 11 Sep 93 13:38 PDT
  5. Message-Id: <m0obbi8-0000YPC@crash.cts.com>
  6. Date: 11 Sep 93 15:36:00 CST
  7. From: "Barry D. Wills" <BWILLS@kirk.safb.af.mil>
  8. To: "amigae" <amigae@bkhouse.cts.com>
  9. Subject: Non-stdin (string input via IDCMP)
  10.  
  11. /*
  12.    This example is in reply to a request from Vinny Elschot.  Sorry is took so
  13.    long, Vinny!
  14.  
  15.    Here's a set of routines that can be used to get a string via IDCMP messages.
  16.    The same concept can be used to get a single character from the keyboard
  17.    without requiring a return to signal end of input as required by Inp() and
  18.    other functions that get charactes from stdin.
  19.  
  20.    The trick here is to get a pointer to an open window so we can change it's
  21.    IDCMP flags to get VANILLAKEY messages from intuition.  Then we just do what
  22.    we will with the VANILLAKEYs, change the windows IDCMP flags back to the way
  23.    they were and return whatever info we've accumulated (in this case a string.)
  24.  
  25.    To extract the scan routine and use it in your progs without modification you
  26.    will need cursorOn(), cursorOff(), and scan(), and all the modules in the
  27.    MODULE directive as a minimum.
  28. */
  29.  
  30. MODULE 'exec/strings',
  31.        'graphics/rastport',
  32.        'intuition/intuition',
  33.        'intuition/screens'
  34.  
  35. RAISE "MEM" IF String()=NIL
  36.  
  37. PROC cursorOn(rp:PTR TO rastport,char)
  38.   DEF x
  39.   SetDrMd(rp,(RP_INVERSVID+RP_JAM2))
  40.   x:=rp.cp_x
  41.   Text(rp,({char}+3),1)
  42.   rp.cp_x:=x
  43. ENDPROC
  44.  
  45. PROC cursorOff(rp:PTR TO rastport,char)
  46.   DEF x
  47.   SetDrMd(rp,RP_JAM2)
  48.   x:=rp.cp_x
  49.   Text(rp,({char}+3),1)
  50.   rp.cp_x:=x
  51. ENDPROC
  52.  
  53. PROC scan(win:PTR TO window,   /* Pointer to window.                   */
  54.           x,y,                 /* Prompt at: (-1,-1=current location.) */
  55.           prompt:PTR TO CHAR,  /* EString, contents=what to display.   */
  56.           answer:PTR TO CHAR)  /* EString, contents=<discarded>.       */
  57. /* EString parameter answer will contain the input string */
  58. /* value, and will be set to the appropriate EString      */
  59. /* length.  The remaining paramters are unchanged.        */
  60.   DEF rp:PTR TO rastport,oldIdcmpFlags,idcmpMessage,idcmpClass,idcmpCode,
  61.       last,i,done=FALSE
  62.   last:=StrMax(answer)-1
  63.   FOR i:=0 TO last DO answer[i]:=0
  64.   i:=0
  65.   rp:=win.rport
  66.   IF x=-1 THEN x:=rp.cp_x ELSE rp.cp_x:=x
  67.   IF y<>-1 THEN rp.cp_y:=y
  68.   Text(rp,prompt,StrLen(prompt))
  69.   x:=rp.cp_x
  70.   cursorOn(rp,32)
  71.   oldIdcmpFlags:=win.flags
  72.   ModifyIDCMP(win,IDCMP_VANILLAKEY)
  73.   REPEAT
  74.     idcmpClass:=WaitIMessage(win)
  75.     SELECT idcmpClass
  76.       CASE IDCMP_VANILLAKEY
  77.         idcmpCode:=MsgCode()
  78.         SELECT idcmpCode
  79.           CASE CR
  80.             done:=TRUE
  81.             SetStr(answer,i)
  82.             WHILE (idcmpMessage:=GetMsg(win.userport)) DO ReplyMsg(idcmpMessage)
  83.             ModifyIDCMP (win,oldIdcmpFlags)
  84.             cursorOff(rp,32)
  85.           CASE BS
  86.             IF i>0
  87.               cursorOff(rp,32)
  88.               answer[i]:=0
  89.               rp.cp_x:=rp.cp_x-rp.txwidth
  90.               DEC i
  91.               cursorOn(rp,32)
  92.             ENDIF
  93.           DEFAULT
  94.             IF (i <= last) AND (idcmpCode>=32) AND (idcmpCode<=126)
  95.               answer[i]:=idcmpCode
  96.               cursorOff(rp,answer[i])
  97.               rp.cp_x:=rp.cp_x+rp.txwidth
  98.               INC i
  99.               cursorOn(rp,32)
  100.             ENDIF
  101.         ENDSELECT
  102.       DEFAULT;
  103.         WriteF('scan():  Unknown IDCMP class=\d\n',idcmpClass)
  104.     ENDSELECT
  105.   UNTIL done
  106. ENDPROC
  107.   /* scan */
  108.  
  109. PROC clearLine(rp:PTR TO rastport,x,y,length)
  110.   DEF i
  111.   rp.cp_x:=x;rp.cp_y:=y
  112.   FOR i := 1 TO length DO Text(rp,' ',1)
  113. ENDPROC
  114.   /* clearLine */
  115.  
  116. PROC display(rp:PTR TO rastport,x,y,s)
  117.   IF StrLen(s)
  118.     IF x>-1 THEN rp.cp_x:=x
  119.     IF y>-1 THEN rp.cp_y:=y
  120.     Text(rp,s,StrLen(s))
  121.   ENDIF
  122. ENDPROC
  123.   /* display */
  124.  
  125. PROC main() HANDLE
  126.   DEF msgWin=NIL:PTR TO window,
  127.       prompt=NIL:PTR TO CHAR,
  128.       answer=NIL:PTR TO CHAR,
  129.       rp=NIL:PTR TO rastport,
  130.       x,y,done=FALSE
  131.   IF (msgWin:=OpenW(10,20,200,21,NIL,WFLG_WINDOWACTIVE+WFLG_DRAGBAR,
  132.                     'Scan A String',NIL,WBENCHSCREEN,NIL))<>NIL
  133.     prompt:=String(8)
  134.     answer:=String(11)
  135.     StrCopy(prompt,'Q=quit: ',ALL)
  136.     rp:=msgWin.rport
  137.     x:=4;y:=17
  138.     SetTopaz(8)
  139.     REPEAT
  140.       scan(msgWin,x,y,prompt,answer)
  141.       IF answer[]=0
  142.         StrCopy(answer,'Speechless?',ALL)
  143.       ELSEIF (answer[]="q") OR (answer[]="Q")
  144.         StrCopy(answer,'Goodbye',ALL)
  145.         done:=TRUE
  146.       ENDIF
  147.       clearLine(rp,x,y,20)
  148.       display(rp,x,y,answer);display(rp,-1,-1,'!!!')
  149.       Delay(75)
  150.       clearLine(rp,x,y,20)
  151.     UNTIL done
  152.     CloseW(msgWin)
  153.   ENDIF
  154.   CleanUp(0)
  155. EXCEPT
  156.   IF msgWin THEN CloseW(msgWin)
  157.   SELECT exception
  158.     CASE "MEM"; WriteF('Out of memory!\n')
  159.     CASE "WIN"; WriteF('Could not open window!\n')
  160.   ENDSELECT
  161.   CleanUp(exception)
  162. ENDPROC